var ReservationsMyWorkTable = {
    id: "ReservationsMyWorkTable",
    seItem: null,
    table: null,
    layerIndex: -1
};
ReservationsMyWorkTable.initColumn = function () {
    var columns =
        [
            {field: 'selectItem', radio: true},
            {title: '', field: 'id', visible: false, align: 'center', valign: 'middle'},
            {title: '', field: 'customerSexHead ', visible: false, align: 'center', valign: 'middle'},
            {title: '预约号', field: 'reservationsNum', align: 'center', valign: 'middle', width: 110, sortable: true},
            {title: '姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true, width: 120,
                formatter: function (value, row, index) {
                    return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'ReservationsMyWorkTable.reservationsNotDetail(" + '"' + row.id + '"' + ")' ><img src='" + row.customerSexHead + "' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                }
            },
            {title: '手机号码', field: 'customerPhone', align: 'center', valign: 'middle', sortable: true, width: 140},
            {title: '预约门诊', field: 'outpatientName', align: 'center', valign: 'middle', sortable: true, width: 110},
            {title: '回访状态', field: 'statuss', align: 'center', valign: 'middle', width: 110, sortable: true,
                formatter: function (value, row, index) {
                    if (row.status === "到店") {return "<span style='color: green'>" + value + "</span>"} else {return value;}
                }
            },
            {title: '预约时间', field: 'appointmentTime', align: 'center', valign: 'middle', sortable: true, width: 170,
                formatter: function (value, row, index) {
                    if (row.huifang === "" || row.huifang == null) {
                        row.huifang = "无回访详情";
                    } else {
                        row.huifang = ",<b>" + "回访详情：" + row.huifang.replace(/\s{2,}/g, "");
                        var reg = /,<b>/g;
                        var regs = /<\/b>/g;
                        row.huifang = row.huifang.replace(reg, '&#10;');
                        row.huifang = row.huifang.replace(regs, ' ');
                    }
                    return "<span style=' cursor:pointer' title='" + row.huifang + "' onclick='ReservationsMyWorkTable.reservationsNotDetail(" + '"' + row.id + '"' + ")'>" + value.substring(0, 19) + "</span>"
                }
            },
            {title: '建档人', field: 'archiving', align: 'center', valign: 'middle', sortable: true, width: 90},
            {title: '跟进人', field: 'follower', align: 'center', valign: 'middle', sortable: true, width: 90},
            {title: '部门', field: 'department', align: 'center', valign: 'middle', sortable: true, width: 100},
            {title: '建档时间', field: 'filingTime', align: 'center', valign: 'middle', sortable: true, width: 170,
                formatter: function (value, row, index) {
                    return value.substring(0, 19);
                }
            },
            {title: '一级渠道', field: 'oneChannel', align: 'center', valign: 'middle', sortable: true, width: 110},
            {title: '二级渠道', field: 'twoChannel', align: 'center', valign: 'middle', sortable: true, width: 110},
            {title: '三级渠道', field: 'thrChannel', align: 'center', valign: 'middle', sortable: true, width: 130},
            {title: '回访内容', field: 'huifang', align: 'center', valign: 'middle', sortable: true, width: 250,
                formatter: function (value, row, index) {
                    return '<span title="'+value+'">'+value+'</span>'
                },
                cellStyle: function (value, row, index) {
                    return {css: {"overflow":"hidden","white-space":"nowrap","text-overflow":"ellipsis"}}
                }
            },
            {title: '性别', field: 'customerSex', align: 'center', valign: 'middle', width: 80, sortable: true,
                formatter: function (value, row, index) {
                    return value === 1 ? '男' : value === 2 ? '女' : '未知';
                }
            },
            {title: '年龄', field: 'customerAge', align: 'center', valign: 'middle', sortable: true, width: 80},
            {title: '预约病种', field: 'appointmentPayService', align: 'center', valign: 'middle', sortable: true, width: 120},
            {title: '访问病种', field: 'accessDisease', align: 'center', valign: 'middle', sortable: true, width: 110},
            {title: '关键词', field: 'keyWord', align: 'center', valign: 'middle', sortable: true, width: 100},
            {title: '微信号', field: 'wechatNumber', align: 'center', valign: 'middle', sortable: true, width: 100},
        ];
    return columns;
};

/**
 * 查询预约客户列表
 */
ReservationsMyWorkTable.search = function () {
    ReservationsMyWorkTable.table.refresh({query: ReservationsMyWorkTable.formParams()});
    // ReservationsMyWorkTable.tongji();
};
/**
 * 新市场预约表单查询提交参数
 */
ReservationsMyWorkTable.formParams = function () {
    var queryData = {};
    queryData['outpatientName'] = multipleSelection($('#outpatientName').val());
    queryData['department'] = $("#department").val(); //  部门
    queryData['reach'] = $("#marketInputPublic", parent.document).val();  //姓名   电话   微信  预约号
    queryData['appointmentPayService'] = $("#appointmentPayService").val();  //预约项目
    queryData['archiving'] = $("#archiving").val();   //建档人
    queryData['follower'] = $("#follower").val();
    queryData['accessDisease'] = $("#accessDisease").val();  //访问病种
    queryData['oneChannel'] = $("#oneChannel").val();  //一级渠道
    queryData['twoChannel'] = $("#twoChannel").val();    //二级渠道
    queryData['thrChannel'] = $("#thrChannel").val();    //三级渠道
    queryData['statuss'] = multipleSelection($('#statuss').val());
    queryData['status'] = $("#status").val();   //状态
    queryData['startTime'] = $("#startTime").val();   //建档时间
    queryData['area'] = multipleSelection($('#area').val());
    queryData['isReturn'] = $("#isReturn").val();
    return queryData;
};
/**
 * 统计建档未到人数
 */
// ReservationsMyWorkTable.tongji = function () {
//     var param1 = multipleSelection($('#outpatientName').val());
//     var param2 = $("#department").val();   //部门
//     var param3 = $("#appointmentPayService").val(); //预约病种
//     var param4 = $("#archiving").val();  //建档人
//     var param5 = $("#follower").val();  //跟进人
//     var param6 = $("#accessDisease").val();  //访问病种
//     var param7 = $("#oneChannel").val();  //一级渠道
//     var param8 = $("#twoChannel").val();  //二级渠道
//     var param9 = $("#thrChannel").val();  //三级渠道
//     var param10 = multipleSelection($('#statuss').val());
//     var param11 = multipleSelection($('#area').val());
//     var param12 = $("#startTime").val();
//     var param13 = $("#status").val();
//     var param14 = $("#marketInputPublic", parent.document).val();  //姓名   电话   微信  预约号
//     $.ajax({
//         type: "GET",
//         url: Feng.ctxPath + "/reservationsMyWork/tongji",
//         data: {
//             param1: param1, param2: param2, param3: param3,
//             param4: param4, param5: param5, param6: param6,
//             param7: param7, param8: param8, param9: param9,
//             param10: param10, param11: param11, param12: param12,param14:param14,
//             param13: param13
//         },
//         dataType: "json",
//         success: function (data) {
//             $("#daihuifang").html(data[0].countNum);
//             $("#yihuifang").html(data[1].countNum);
//         }
//     });
// };
/**
 * 把对象拼接成URL的键值对参数
 * param1=value1&param2=value2
 */
var parseParam = function (param) {
    var paramStr = "";
    $.each(param, function (i) {paramStr += '&' + getSingleData(param, i);});
    return paramStr.substr(1);
};

var getSingleData = function (param, key) {return key + "=" + param[key];};

ReservationsMyWorkTable.check = function () {// 检查是否选中
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        ReservationsMyWorkTable.seItem = selected[0];
        return true;
    }
};

/**
 * 导出
 */
ReservationsMyWorkTable.reservationsExportWD = function () {
    var param = parseParam(ReservationsMyWorkTable.formParams());
    location.href = Feng.ctxPath + '/reservationsMyWork/reservationsExportWD?' + param;
};

/**
 * 重置
 */
ReservationsMyWorkTable.resetSearch = function () {
    $("#marketInputPublic", parent.document).val("");
    $("#archiving").val("");
    $("#follower").val("");
    $("#accessDisease").val("");
    $("#statuss").selectpicker('deselectAll');
    $("#startTime").val("");
    $("#status").val("");
    $("#appointmentPayService").val("");
    $("#oneChannel").val("请选择");
    $("#twoChannel").val("请选择");
    $("#thrChannel").val("请选择");
    $("#area").selectpicker('deselectAll');
    $("#outpatientName").selectpicker('deselectAll');
    $("#department").val("");
    ReservationsMyWorkTable.search();
};

/**
 * 片区筛选
 */
ReservationsMyWorkTable.getData = function () {
    var area = multipleSelection($('#area').val());
    $.post(Feng.ctxPath + "/dept/getListByArea/"+area,function (data) {
        $('#outpatientName').empty();
        for (var i = 0; i < data.length; i++) {
            $('#outpatientName').append("<option value=" + data[i].simplename + ">" + data[i].simplename + "</option>");
        }
        $('#outpatientName').selectpicker('refresh');
        $('#outpatientName').selectpicker('render');
        ReservationsMyWorkTable.search();
    }, "json");
};
/**
 * 添加跟进人
 */
ReservationsMyWorkTable.getFollower = function () {
    var index = layer.open({
        type: 2,
        title: '添加跟进人',
        area: ['70%', '60%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservations/to_follower'
    });
    this.layerIndex = index;
};

ReservationsMyWorkTable.getArchiving = function () { // 选择建档人
    var index = layer.open({
        type: 2,
        title: '选中建档人',
        area: ['70%', '60%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservation/to_archiving'
    });
    this.layerIndex = index;
};

ReservationsMyWorkTable.reservationsNotDetail = function (id) { // 点击表格姓名进入详情 - 未到
    var pageType = $("#pageType").val();
    window.localStorage.setItem("c", pageType);
    var index = layer.open({
        type: 2,
        title: '预约客户详情',
        area: ['100%', '100%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservationsMyWork/to_reservationsNotDetail/' + id
    });
    this.layerIndex = index;
};

ReservationsMyWorkTable.timeChange = function (obj) { // 时间改变是搜索列表
    var time = $("#startTime").val();
    var date = new Date(time);
    if(obj == '+'){
        date.setDate(date.getDate() + 1);
    }else {
        date.setDate(date.getDate() - 1);
    }
    var month = date.getMonth() + 1;
    var day = date.getDate();
    if(month < 10){month = "0" + month}
    if(day < 10){day = "0" + day}
    $("#startTime").val(date.getFullYear() + "-" + month + "-" + day);
    ReservationsMyWorkTable.search();
};

ReservationsMyWorkTable.waitingReturn = function () { // 点击上面状态筛选表格
    document.getElementById("isReturn").value = "1";
    ReservationsMyWorkTable.search();
    $("#waitingReturn").addClass('actionClick').siblings().removeClass('actionClick');
};

ReservationsMyWorkTable.returned = function () {
    document.getElementById("isReturn").value = "2";
    ReservationsMyWorkTable.search();
};

$("#marketSearchAll", parent.document).click(function () {ReservationsMyWorkTable.search();});//主页搜索

function phonePrivileges(){   //隐藏手机号
    var phonePrivileges=parent.$("#phonePrivileges").val();
    if(phonePrivileges == 2){
        $('#ReservationsMyWorkTable').bootstrapTable('hideColumn', 'customerPhone');
    }
}
ReservationsMyWorkTable.time = function () { // 初始化赋值时间
    $("#startTime").val(setDateTime(new Date(),0));
};

var scollPostion = 0;
ReservationsMyWorkTable.getScrollPosition = function (){ // 获取表格滚动到什么地方
    scollPostion = $('#ReservationsMyWorkTable').bootstrapTable('getScrollPosition');
};

ReservationsMyWorkTable.scrollTo = function (){ // 指定表格滚动到什么地方
    $("#ReservationsMyWorkTable").on('load-success.bs.table',function(data){
        $('#ReservationsMyWorkTable').bootstrapTable('scrollTo', scollPostion);
    });
};

$(function () {
    ReservationsMyWorkTable.time();
    var defaultColunms = ReservationsMyWorkTable.initColumn();
    var table = new BSTable(ReservationsMyWorkTable.id, "/reservationsMyWork/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(ReservationsMyWorkTable.formParams());
    ReservationsMyWorkTable.table = table.init();
    ReservationsMyWorkTable.waitingReturn();
    menuNameHideColumn($("#menuName").val(),'ReservationsMyWorkTable');
    $(document).on('click','#laydate_clear,#laydate_today,#laydate_ok',function () {ReservationsMyWorkTable.search()});
    $("#oneChannel").change(function () {$("#twoChannel").val('请选择');$("#thrChannel").val('请选择');ReservationsMyWorkTable.search();});
    $("#twoChannel").change(function () {$("#thrChannel").val('请选择');ReservationsMyWorkTable.search();});
    $("#thrChannel").change(function () {ReservationsMyWorkTable.search();});
    checkDate = function () {ReservationsMyWorkTable.search()};
    callbackTable = function () {ReservationsMyWorkTable.search()};
    phonePrivileges();
});